Repository navigation
fix(connections): keep retrying when Google does not confirm a revoked grant - #22669
cryppadotta wants to merge 17 commits into
Conversation
…d grant Owner decision 2026-11-23: when Google access must end and Google cannot be reached or confirm, the app keeps the refresh token in a pending revocation record and retries until Google answers. Paperclip Cloud still never stores provider tokens. Before this change, removing a Paperclip-managed Google connection or grant deleted the refresh token whatever Paperclip Cloud answered, so a failed or unconfirmed revoke could never be retried and Google access stayed open. - google_pending_revocations holds the token's local_encrypted material (copied from the connection secret version, never plaintext), keyed by the token hash, with company_id and no foreign key. - Removal queues the token before the broker call, so a restart during the call still resumes; a final answer deletes the row, a retryable one keeps it and the removal reports "retrying". - The google-pending-revocation delivery queue retries with a lease, 1 minute to 6 hours backoff and a 30-day hard stop, and writes an audit event and an activity entry when it ends. - Company deletion queues every managed Google token inside the delete transaction; a Cloud-removed instance queues them, then drops the connection secrets. - The removal message says Paperclip keeps trying, with no action needed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…l result type Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…mber Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Feynman Nuclear Review — 5/5Re-review of 0574c31The PR's feature code is unchanged from the 5/5 head 21204be. The only differences in the current PR file set are bookkeeping: master added a new The GitHub Residual notes, not defects: renaming a not-yet-merged migration changes its tag, so confirm no environment already applied the old Verdict: no actionable correctness, security, or reliability defects found within the stated coverage. Advisory only. Score 5 because the feature code and tests are unchanged from the previously published 5/5 head and the only change is a correct migration renumber. Evidence: the current PR file set has 31 files; comparing its per-file add/delete stats to the prior head shows only Reviewed commit: Coverage: 31 files. Task: PAP-6439 · Run · Review history |
Ponytail Paperclip Review — 4/5What this change does: A managed Google connection removal keeps the encrypted refresh token in Prior findings: the earlier reliability, tenant, live-use, instance-removal, pinned-version, transactional-sweep, admin-gate, and activity-feed fixes remain; the PR's code and tests for the retry behavior are unchanged. The renumbered migration and snapshot are structurally consistent: Nice to have
Verdict: ship; the two items above are optional. Not checked: tests and typecheck were not run (no dependencies or database); CI/check status was unavailable; no live broker or Google call was made; the migration renumber was verified structurally, not applied to a database. Score 4: this head is the previous assessment plus a correct migration renumber. I verified that master's new 0502 snapshot id is Reviewed commit: Coverage: 31 files. Task: PAP-6440 · Run · Review history |
[High impact] The change reaches managed Google access removal and cleanup across Paperclip companies. Fix both pinned-version gaps before merging so removed Google access is reliably revoked. FindingsFix with agent prompt### Issue 1
server/src/services/tool-access/connections/google-pending-revocation.ts:170-174
`queueCompanyGoogleRevocations` always copies `secret.latestVersion`, even when a grant pins an older `versionSelector`. Company deletion or removed-instance cleanup then deletes the source secret. The worker retries a different token and can no longer revoke the token the grant used.
Select each grant's pinned version when present, and deduplicate by secret and selected version rather than secret alone.
**How this was verified:** Grant resolution honors numeric `versionSelector` values, but bulk cleanup copies only `latestVersion` before deleting the source secrets.
### Issue 2
server/src/services/tool-access/connections/google-pending-revocation.ts:191-200
`heldByLiveSecret` compares the pending token with a secret's latest version, then treats any live reference to that secret as using that token. A live grant may pin a different version. If the latest token matches the pending token but the grant selects another, the worker deletes the retry as `other_connections_remain` without calling Google. The reverse mismatch can revoke a token that a live grant still uses.
Check each live reference's selected version before deciding whether its token is in use.
**How this was verified:** Live references are matched only by `secretId`, while the token hash comes only from `latestVersion`; grant resolution separately honors pinned versions.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.SummaryThe PR saves encrypted Google tokens for background revocation retries, including company deletion and confirmed instance removal.
Reviews (8) · Last reviewed commit: "chore(runner-e2e): refresh pinned source..." · Reviewed by Greptile |
…d, and act only on a confirmed instance removal A restart between the first broker call and the secret delete left the removed grant's own secret live, and the worker read it as another live holder and dropped the retry. The pending row now records its source secret and the live-holder check skips it. A 401 from Paperclip Cloud's status check no longer starts the instance removal cleanup; only a signed status: removed answer does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…vocation * origin/master: fix(antigravity): offer the cloud candidate in the agent picker for linux-x64 environments (#22655) fix(cloud): name every durable-data blocker in the online pass result (#22641) fix(workspaces): move a workspace with only a task warning online, as offline already does (#22650)
…dge live use by references The pending row is now unique per company and token, and settling or dropping it is filtered by company, so one company's removal can never settle another company's retry. A live holder is now a secret that a live grant or connection refers to; a stored but unused copy, or a secret whose delete failed, no longer cancels the retry. The removed instance cleanup no longer runs inside the status request. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@greptileai review |
…nd limit instance cleanup to instance admins The live-use check now counts any secret with the same token that a live grant or connection uses, including the row's source secret, so a grant signed in again before the retry is never revoked under it. Rows queued for a removed instance skip the check. Only an instance admin's status check starts the removed-instance cleanup, and only one sweep runs at a time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…vocation * origin/master: test(native): give the repeated-bytes provider pack tests a 60s timeout (#22663)
|
@greptileai review |
…removed-instance sweep atomically A grant that pins an older refresh-token version sends that version, so the retry now keeps the same version instead of the latest one. The removed-instance cleanup queues every company's tokens in one transaction before it drops any secret, and the status request waits for that save; the in-process single-flight guard is gone because the queue is idempotent. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@greptileai review |
…nce cleanup Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nection's Activity tab Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nt types Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@greptileai review |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| const id = await insertFromSecret(tx, { | ||
| companyId, connectionId: connection.id, grantId: grant.id, subject, profile: profile.id, | ||
| secretId, version: secret.latestVersion, reason, nextAttemptAt: now, now, | ||
| }); | ||
| if (id) queued.push({ secretId, version: secret.latestVersion }); |
There was a problem hiding this comment.
queueCompanyGoogleRevocations always copies secret.latestVersion, even when a grant pins an older versionSelector. Company deletion or removed-instance cleanup then deletes the source secret. The worker retries a different token and can no longer revoke the token the grant used.
Select each grant's pinned version when present, and deduplicate by secret and selected version rather than secret alone.
How this was verified: Grant resolution honors numeric versionSelector values, but bulk cleanup copies only latestVersion before deleting the source secrets.
Knowledge Base Used: Secrets and credentials
Prompt To Fix With AI
This is a comment left during a code review.
Path: server/src/services/tool-access/connections/google-pending-revocation.ts
Line: 170-174
Comment:
**Cleanup saves the wrong token**
`queueCompanyGoogleRevocations` always copies `secret.latestVersion`, even when a grant pins an older `versionSelector`. Company deletion or removed-instance cleanup then deletes the source secret. The worker retries a different token and can no longer revoke the token the grant used.
Select each grant's pinned version when present, and deduplicate by secret and selected version rather than secret alone.
**How this was verified:** Grant resolution honors numeric `versionSelector` values, but bulk cleanup copies only `latestVersion` before deleting the source secrets.
**Knowledge Base Used:** [Secrets and credentials](https://app.greptile.com/-/switch-tenant?tenant=paperclip-org-3&next=%2Fcustom-context%2Fknowledge-base%2Fpaperclipai%2Fpaperclip%2F-%2Fdocs%2Fsecrets-and-credentials.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.| .innerJoin(companySecretVersions, and( | ||
| eq(companySecretVersions.secretId, companySecrets.id), | ||
| eq(companySecretVersions.version, companySecrets.latestVersion), | ||
| )) | ||
| .where(and( | ||
| eq(companySecrets.companyId, row.companyId), | ||
| isNull(companySecrets.deletedAt), | ||
| ne(companySecrets.status, "deleted"), | ||
| eq(companySecretVersions.valueSha256, row.tokenSha256), | ||
| liveSecretReference(sql`${companySecrets.id}`, sql`${companySecrets.companyId}`), |
There was a problem hiding this comment.
Retry checks the wrong version
heldByLiveSecret compares the pending token with a secret's latest version, then treats any live reference to that secret as using that token. A live grant may pin a different version. If the latest token matches the pending token but the grant selects another, the worker deletes the retry as other_connections_remain without calling Google. The reverse mismatch can revoke a token that a live grant still uses.
Check each live reference's selected version before deciding whether its token is in use.
How this was verified: Live references are matched only by secretId, while the token hash comes only from latestVersion; grant resolution separately honors pinned versions.
Knowledge Base Used: Secrets and credentials
Prompt To Fix With AI
This is a comment left during a code review.
Path: server/src/services/tool-access/connections/google-pending-revocation.ts
Line: 191-200
Comment:
**Retry checks the wrong version**
`heldByLiveSecret` compares the pending token with a secret's latest version, then treats any live reference to that secret as using that token. A live grant may pin a different version. If the latest token matches the pending token but the grant selects another, the worker deletes the retry as `other_connections_remain` without calling Google. The reverse mismatch can revoke a token that a live grant still uses.
Check each live reference's selected version before deciding whether its token is in use.
**How this was verified:** Live references are matched only by `secretId`, while the token hash comes only from `latestVersion`; grant resolution separately honors pinned versions.
**Knowledge Base Used:** [Secrets and credentials](https://app.greptile.com/-/switch-tenant?tenant=paperclip-org-3&next=%2Fcustom-context%2Fknowledge-base%2Fpaperclipai%2Fpaperclip%2F-%2Fdocs%2Fsecrets-and-credentials.md)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
Thinking Path
Linked Issues or Issue Description
Refs #22065
What happened?
A managed Google connection was removed while Paperclip Cloud or Google was unavailable. The removal result was
not_ended, and the refresh token was deleted. Paperclip could not try again, so Google access stayed open.What did you expect?
Paperclip keeps the token only for the retry, and retries automatically until Google confirms or gives a final answer. The user sees that Paperclip keeps trying, and does not need to act.
Steps to reproduce
What Changed
google_pending_revocations(migration0502, additive,CREATE TABLEonly). It hascompany_idwith no foreign key, so a row outlives a deleted company. It stores the token as the samelocal_encryptedmaterial as the connection secret version. It never stores plaintext. It is not a company secret: it is not listed, an agent cannot bind it, and no route reads it.google-access-removal.ts: before the broker call, the app copies the sent token version into a pending row (lease 2 minutes, so a restart during the call resumes). A final outcome deletes the row. A retryable outcome (provider_revocation_failed,revocation_in_progress,broker_unavailable,broker_error,unexpected_error) keeps it, and the result is the newretrying. If the connection secret is still live after the conditional delete (for example, the user signed in again during the call), the row is deleted.google-pending-revocationdelivery queue (registerDeadlineWork; it registers the real next deadline and has no deadline when the table is empty). Each attempt takes a lease on the row and calls the broker with a 15-second timeout. Backoff is 1, 2, 4 ... minutes, up to 6 hours. The hard stop is 30 days. The worker stops without a call if a live company secret holds the same token.connection.google_access_retryaudit event and atool_connection.google_access_endedortool_connection.google_access_not_endedactivity entry (labels added to the activity feed). For a deleted company, it writes only a log line.reason = company_deleted).removed, the app queues every managed Google token (reason = instance_removed), then deletes each connection secret.ManagedGoogleAccessRemoval.resultgetsretrying(shared type and OpenAPI schema). The removal message forretrying: "Google didn't confirm that Paperclip's access has ended yet. Paperclip will keep trying automatically." It has no link and asks for no action.doc/connections/GOOGLE-WORKSPACE.md("Pending revocation"),docs/api/tool-connections.md, the help pagedocs/guides/board-operator/deleting-connection-data.md, and a dated decision row indoc/memory/google-connector-memory.md.tests/runner-e2e/source-sdk-public-inputs.jsonrefreshed for the changed shared type, aspnpm verifyasks.Verification
npx vitest run src/__tests__/google-managed-connection-removal.test.ts(embedded Postgres): 46 pass. Without the fix, 15 fail (red), with the fix all pass (green). New cases:binding_not_known,other_connections_remainandprovider_revocation_disabledstop the retry;403 INSTANCE_SUSPENDEDis retried;npx vitest run src/__tests__/paperclip-cloud-connector-status.test.ts: the removed-instance hook runs only forremoved, and a failure does not change the status.ui:npx vitest run src/features/connections/googleAccessRemoval.test.ts(7 pass, the newretryingcase is red without the change).tool-connection-content-deletion, its canary,delivery-work-coordinator,idle-local-work,packages/dbtests;servertsc --noEmit.pnpm verifyexits 2 (global doc input changed), so required CI supplies the broad coverage.Risks
CREATE TABLEand indexes on the new table):online_compatible.403and the app retries until the hard stop. The Cloud broker change that accepts it is a separate PR in the private Cloud repository.Model Used
claude-opus-5-5) through Claude Code, with tool use (shell, file edits, embedded Postgres tests), standard reasoning.Checklist
Fixes: #/Closes #/Refs #OR (b) described the issue in-PR following the relevant issue template#NNN/github.com/paperclipai/paperclipURLs)docs/...,fix/...) and contains no internal Paperclip ticket id or instance-derived details🤖 Generated with Claude Code